package com.hhd.pdf.components.functions;

import com.hhd.pdf.CommonUtils;
import org.springframework.stereotype.Component;

@Component
public class EqFunction implements PdfFunction<Boolean> {
    @Override
    public Boolean execute(PdfFunctionContext context) {
        Object var1 = context.getVariable("var1");
        Object var2 = context.getVariable("var2");

        if (var1 == var2) return true;
        if (CommonUtils.isEmpty(var1) && !CommonUtils.isEmpty(var1)) return false;
        if (!CommonUtils.isEmpty(var1) && CommonUtils.isEmpty(var1)) return false;

        if((var1 instanceof String ? CommonUtils.isEmpty((String)var1) : CommonUtils.isEmpty(var1))
                && (var2 instanceof String ? CommonUtils.isEmpty((String)var2) : CommonUtils.isEmpty(var2)))
            return true;

        if (var1 instanceof Number) {
            var1 = var1.toString();
        }

        if (var2 instanceof Number) {
            var2 = var2.toString();
        }

        return var1.equals(var2);
    }

    @Override
    public String getFunctionName() {
        return "eq";
    }
}
